package com.unittec.drainage.provider.auth.service;

import java.util.Date;

import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.unittec.drainage.provider.auth.entity.SysLimitCall;
import com.unittec.drainage.provider.auth.mapper.LimitCallMapper;
import com.unittec.drainage.provider.root.ServiceRoot;
import com.unittec.drainage.provider.scope.ThreadLocalScope;
import com.unittec.drainage.sdk.root.common.FieldConstants;

/**
 * @author: tangsicong
 * @date: 2019-6-25
 */
@Component
public class LimitCallService extends ServiceRoot<LimitCallMapper, SysLimitCall> {
    public boolean saveLimitCall(SysLimitCall limitCall) {
        return save(limitCall);
    }

    @Cacheable(value = "xxzyt-auth-100",
        key = "T(com.unittec.drainage.provider.scope.ThreadLocalScope).appId+':LimitCall'+ #id",
        condition = "T(com.unittec.drainage.provider.scope.ThreadLocalScope).appId != null")
    public SysLimitCall getLimitCall(String id) {
        return getOne(new LambdaQueryWrapper<SysLimitCall>().eq(SysLimitCall::getId, id)
            .or(i -> i.eq(SysLimitCall::getTenantId, FieldConstants.SYS_TENANT_ID)
                .eq(SysLimitCall::getAppId, FieldConstants.SYS_APP_ID).eq(SysLimitCall::getId, id)));

    }

    @CacheEvict(value = "xxzyt-auth-100",
        key = "T(com.unittec.drainage.provider.scope.ThreadLocalScope).appId+':LimitCall'+ #limitCallId",
        condition = "T(com.unittec.drainage.provider.scope.ThreadLocalScope).appId != null")
    public boolean deleteFunctionById(String limitCallId) {
        return removeById(limitCallId);
    }

    @CacheEvict(value = "xxzyt-auth-100",
        key = "T(com.unittec.drainage.provider.scope.ThreadLocalScope).appId+':LimitCall'+ #limitCall",
        condition = "T(com.unittec.drainage.provider.scope.ThreadLocalScope).appId != null")
    public boolean updateLimitCall(SysLimitCall limitCall) {
        limitCall.setModifyTime(new Date());
        limitCall.setModifyUser(ThreadLocalScope.getAuthData().getUser());
        return updateById(limitCall);
    }

}
